<?php

namespace app\index\controller;

use app\common\controller\Frontend;
use think\Db;
use think\Request;

class Xiangsuzhuangbei extends Frontend
{

    protected $noNeedLogin = '*';
    protected $noNeedRight = '*';
    protected $layout = 'default';

    public function index()
    {
		$this->view->assign('title', __('橡塑装备'));
		//轮播图
		$banner_type_id=Db::name('bannertype')->where(['type'=>0,'name'=>'橡塑装备'])->column('id');

		$rs_banner=Db::name('banner')->where([
			'type_data'=>0,
			'bannertype_id'=>$banner_type_id[0],
		])->order('weigh desc')->select();
		$this->assign('rs_banner',$rs_banner);

		$nav=Db::name('category')->where(['type'=>'product','pid'=>0,'status'=>'normal'])->order('weigh desc')->limit(6)->select();


		$qiye=Db::name('qiye')->orderRaw('RAND()')->where(['name'=>['<>','']])->order('createtime desc')->select();

		$this->assign('qiye',$qiye);

		foreach($nav as $k=>$v){
			$child_ids=getChildIds($v['id']);

			$product_list=Db::name('product')->alias('p')->field('p.*,q.name as qiye_name')
			->join('qiye q','p.qiye_id=q.id','LEFT')->orderRaw('rand()')->where(['p.category_id'=>['in',$child_ids]])->limit(8)->select();

			$nav[$k]['list']=$product_list;
		}

		$this->assign('nav',$nav);
        return $this->view->fetch();
    }

	public function search(Request $request){
		$keyword=$request->param('keyword/s','');
		$rs_qiye=Db::name('qiye')->where(['name'=>['like',"%$keyword%"]])->select();
		$rs_product=Db::name('product')->alias('p')->where(['p.name'=>['like',"%$keyword%"]])->field('p.*,q.name as qiye_name')
		->join('qiye q','p.qiye_id=q.id','LEFT')
		->select();

		$this->assign('keyword',$keyword);
		$this->assign('rs_qiye',$rs_qiye);
		$this->assign('rs_product',$rs_product);
		$this->assign('empty','<div class="empty">
					<div>
						<img src="/mobile/img/ico_empty.png" >
						<p>没有查询到对应内容</p>
					</div>
				</div>');
		return $this->view->fetch();
	}

	public function product_detail(Request $request){
		$this->view->assign('title', __('产品详情'));

		$id=$request->param('id/d',0);
		$rs=Db::name('product')->alias('p')
		->join('qiye q','p.qiye_id=q.id','LEFT')->field('p.*,q.name as qiye_name')
		->order('weigh desc,updatetime desc')->where(['p.id'=>$id])->find();

		if(is_null($rs)){
			$this->error('内容不存在');
		}
		if($rs['images']==''){
			$rs['images']=[];
		}else{
			$rs['images']=explode(',',$rs['images']);
		}

		$this->assign('rs',$rs);

		return $this->view->fetch();
	}

	public function qiye_detail(Request $request){
		$this->view->assign('title', __('企业详情'));

		$id=$request->param('id/d',0);
		$rs=Db::name('qiye')->where(['id'=>$id])->order('weigh desc')->find();
		$rs_products=Db::name('product')->where([
			'status'=>2,
			'qiye_id'=>$rs['id']
		])->select();


		$rs['products']=$rs_products;

		if(is_null($rs)){
			$this->error('内容不存在');
		}
		$this->assign('rs',$rs);
		$this->assign('empty','<div class="empty">
						<div>
							<img src="/mobile/img/ico_empty.png" >
							<p>没有查询到对应内容</p>
						</div>
					</div>');

		return $this->view->fetch();
	}

	public function category(Request $request){
		$this->view->assign('title', __('产品分类'));
		$id=$request->param('id/d',0);
		$category=Db::name('category')->where('id',$id)->find();
		$parent_category=Db::name('category')->where('id',$category['pid'])->where('status','normal')->find();
		$brother=Db::name('category')->where('pid',$parent_category['id'])->where('status','normal')->select();

		$children_ids=getChildIds($id);
        // 添加自己
        array_push($children_ids,$id);
		$product=Db::name('product')->alias('p')
		->join('qiye q','p.qiye_id=q.id','LEFT')->field('p.*,q.name as qiye_name')
		->where(['p.status'=>'2','p.category_id'=>['in',$children_ids]])->order('weigh desc,updatetime desc')
            ->paginate(20);

		$this->assign('parent_category',$parent_category);
		$this->assign('category',$category);
		$this->assign('brother',$brother);
		$this->assign('product',$product);

		$qiye=Db::name('qiye')->where(['name'=>['<>','']])->limit(14)->order('createtime desc')->select();

		$this->assign('qiye',$qiye);
		return $this->view->fetch();
	}
}
